export default {
  name: 'sandboxIndex',
  data () {
    return {
      disabled: false,
      sendData: {
    		keyList: [],
    	},
      form: {
		    requestUrl: '',
        apiType: "",
        apiOptions:  [],
        clearCode: '',
        keyList: [],
        applyList: []
      }
    }
  },
  created () {
    this.getList();
  },
  computed: {
    isGet() {
		//console.log(this.form.apiType);
		if(this.form.apiType === '' ) {
		  return true;
		} else {
		  return false;
		}
	}
  },
  methods: {
    getList() {//页面加载获取列表
      this.axios.get(this.path+'/laiyifen-sandboxs/getAllInterface')
      .then((response) => {
        if(response.status === 200){
          // console.log((response.data));
          this.form.apiOptions = response.data.list;
        }
      })
      .catch((error) => {
        alert(error);
      });
    },
    getKeys() {
      //下拉列表改变时发送ajax请求获取数据
      // 发送一个类型
      // console.log(this.form.apiType);
  	  this.form.apiOptions.forEach((item) => {
    		if(item.interfaceName === this.form.apiType) {
    			this.requestUrl = item.localhostUrl;
    		}
  	  });
      this.axios.post(this.path+'/laiyifen-sandboxs/getIterface?interfaceName='+this.form.apiType)
      .then((response) => {
    	    if(response.status === 200) {
    			//console.log(response.data);
      			this.form.keyList =  response.data.sys;
      			this.form.applyList =  response.data.app;
      		}
      })
      .catch((error) => {
        alert(error);
      })
    },
    getAll() {
      let send=this.setPut(this.clearAllData);
	  console.log(send);
      this.axios.post(this.path+'/laiyifen-sandboxs/requestUrl', send)
      .then((response) => {
    		if(response.status === 200){
    			// console.log(response.data);
          this.auth.setResult(response.data);
          this.$store.commit('getRes', response.data);
          this.$router.push('/result');
    		}
      })
      .catch((error) => {
        alert(error);
      })
    },
    getSign(){
      let send=this.setPut(this.clearData);
      this.axios.post(this.path+'/laiyifen-sandboxs/getSign', send)
      .then((response) => {
    	  // console.log(response);
    		if(response.status === 200) {
    			this.setSign(response.data);
    		}
      })
      .catch((error) => {
        alert(error);
      })
    },
    setPut(fn) {
      // 处理发送的数据为对象形式
	  this.sendData = {
		keyList: []
	  };
      this.sendData['apiType']=this.form.apiType;
      this.sendData['applyList']=this.form.applyList;
      this.form.keyList.forEach(fn);
      console.log(this.sendData);
      return this.sendData
    },
    clearData(item) {
      // 点击获取sign的业务
	  // console.log(item.paramName,item.value)
      if(item.paramName !== 'sign') {
        this.sendData.keyList.push(item);
      }
    },
    clearAllData(item) {
	    this.sendData['requestUrl'] = this.requestUrl;
      this.sendData.keyList.push(item);
    },
    setSign(data) {
      this.form.keyList.forEach((item) => {
        if(item.paramName === 'sign') {
          item.value = data;
        }
      });
    }
  }
}
